﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Eneta.Portal.Common;
using Eneta.Portal.Common.Repository;
using Microsoft.SharePoint;

namespace Eneta.Portal.Companies.WebParts
{
    /// <summary>
    /// Class for IO products dropdown menu.
    /// </summary>
    [Guid("bd40bde7-8bb4-463e-bad5-ebb678de890a")]
    public class IOProductsDropDown : WebPart
    {
        protected DropDownList _productsDropDown;

        /// <summary>
        /// Creates dropdown control.
        /// </summary>
        protected override void CreateChildControls()
        {
            Controls.Add(new LiteralControl("<H3><a>"+Resources.Companies.Products+"</a></H3>"));
            Controls.Add(new LiteralControl("<fieldset>"));

            _productsDropDown = new DropDownList();
            _productsDropDown.ID = "_productsDropDown";
            _productsDropDown.DataTextField = "Title";
            _productsDropDown.DataValueField = "Id";
            _productsDropDown.AutoPostBack = true;
            _productsDropDown.SelectedIndexChanged += ProductsDropDownSelectedIndexChanged;
            Controls.Add(_productsDropDown);

            Controls.Add(new LiteralControl("</fieldset>"));

            base.CreateChildControls();
        }

        void ProductsDropDownSelectedIndexChanged(object sender, EventArgs e)
        {
            var l1 = Request.QueryString["l1"];
            var l2 = Request.QueryString["l2"];
            var pId = _productsDropDown.SelectedValue;

            var urlParams = new List<string>();

            if (!string.IsNullOrEmpty(l1))
            {
                urlParams.Add("l1=" + l1);
            }
            if (!string.IsNullOrEmpty(l2))
            {
                urlParams.Add("l2=" + l2);
            }
            if (!string.IsNullOrEmpty(pId))
            {
                urlParams.Add("p=" + pId);
            }

            Response.Redirect("Default.aspx?" + string.Join("&", urlParams.ToArray()));
        }

        /// <summary>
        /// Binds dropdown control to data.
        /// </summary>
        /// <param name="e">Event arguments.</param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnLoad(e);

            EnsureChildControls();
            try
            {
                //var products = CompanyQueryManager.GetProducts();
                var repository = new CompanyRepository(SPContext.Current.Web);
                var products = repository.GetProductResults();
                _productsDropDown.DataSource = products;
                _productsDropDown.DataBind();

                var item = new ListItem(Resources.Companies.SelectProductDropDownLabel, string.Empty);
                _productsDropDown.Items.Insert(0, item);

                var productId = Request.QueryString["p"] ?? string.Empty;
                if (_productsDropDown.Items.FindByValue(productId) != null)
                    _productsDropDown.SelectedValue = productId;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
        }

        /// <summary>
        /// Avoids additional begin tag of web part.
        /// </summary>
        /// <param name="writer"></param>
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
        }

        /// <summary>
        /// Avoids additional end tag of web part.
        /// </summary>
        /// <param name="writer"></param>
        public override void RenderEndTag(HtmlTextWriter writer)
        {
        }

        private HttpRequest Request
        {
            get
            {
                return HttpContext.Current.Request;
            }
        }

        private HttpResponse Response
        {
            get
            {
                return HttpContext.Current.Response;
            }
        }
    }
}
